/**
 * Name        : JavaMapVer3
 * Version     : 
 * Vendor      : 
 * Description : 
 */

#include "JavaMapVer3.h"
#include "../inc/Ui/WebForm.h"
#include "../inc/Ui/GoogleBuzz/GoogleBuzzMenuForm.h"
#include "../inc/Ui/FindMultipointDirectionForm.h"

using namespace Osp::App;
using namespace Osp::Base;
using namespace Osp::System;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;

JavaMapVer3::JavaMapVer3() {
}

JavaMapVer3::~JavaMapVer3() {

}

Application*
JavaMapVer3::CreateInstance(void) {
	// Create the instance through the constructor.
	return new JavaMapVer3();
}

bool JavaMapVer3::OnAppInitializing(AppRegistry& appRegistry) {
	// TODO:
	// Initialize UI resources and application specific data.
	// The application's permanent data and context can be obtained from the appRegistry.
	//
	// If this method is successful, return true; otherwise, return false.
	// If this method returns false, the application will be terminated.

	// Uncomment the following statement to listen to the screen on/off events.
	//PowerManager::SetScreenEventListener(*this);

	Frame *pFrame = null;
	result r = E_SUCCESS;

	pFrame = GetAppFrame()->GetFrame();

	// Create a form
	FormManager *pFormManager = null;
	r = FormManager::GetInstance(pFormManager);
	if (IsFailed(r)) {
		return false;
	}
	pFormManager->SetFrame(pFrame);

	//create webform
	//Form *pForm = null;
	WebForm *pWebForm = new WebForm();
	r = pWebForm->Construct(FORM_STYLE_SOFTKEY_0 | FORM_STYLE_SOFTKEY_1
			| FORM_STYLE_OPTIONKEY | FORM_STYLE_INDICATOR);
	pWebForm->SetName(L"WebForm");
	if (IsFailed(r))
	{
		AppLog("WebForm Construct() has failed.\n");
		return false;
	}
	pWebForm->SetFormId(0);

	r = pFormManager->Add(WebForm::GetFormId(), pWebForm);
	if (IsFailed(r)) {
		return false;
	}
	AppLog("Create Webfomr success");

	// create ChangeMapTypeForm
	int count = 1;

	ChangeMapTypeForm *pChangeMapTypeForm = new ChangeMapTypeForm();
	pChangeMapTypeForm->Initialize();
	pChangeMapTypeForm->SetFormId(count);
	r = pFormManager->Add(ChangeMapTypeForm::GetFormId(), pChangeMapTypeForm);
	if (IsFailed(r)) {
		return false;
	}
	AppLog("Create ChangeMapType Success");

	// create FindLocationForm
	count ++;
	FindLocationForm *pFindLocationForm = new FindLocationForm();
	pFindLocationForm->Initialize();
	pFindLocationForm->SetFormId(count);
	r = pFormManager->Add(FindLocationForm::GetFormId(), pFindLocationForm);
	if (IsFailed(r)) {
		return false;
	}
	AppLog("Create findlocation success");

	// create FindDirection
	count ++;
	FindDirectionForm *pFindDirectionForm = new FindDirectionForm();
	pFindDirectionForm->Initialize();
	pFindDirectionForm->SetFormId(count);
	r = pFormManager->Add(FindDirectionForm::GetFormId(), pFindDirectionForm);
	if (IsFailed(r)) {
		return false;
	}

	//create WeatherForm
	count ++;
	WeatherForm *pWeatherForm = new WeatherForm();
	pWeatherForm->Initialize();
	pWeatherForm->SetFormId(count);
	r = pFormManager->Add(WeatherForm::GetFormId(), pWeatherForm);
	if (IsFailed(r)) {
		return false;
	}
	AppLog("Create WeatherForm Success");

	count ++;
	WeatherInfoForm *pWeatherInfoForm = new WeatherInfoForm();
	pWeatherInfoForm->Initialize();
	pWeatherInfoForm->SetFormId(count);
	r = pFormManager->Add(WeatherInfoForm::GetFormId(), pWeatherInfoForm);
	if (IsFailed(r)) {
		return false;
	}
	AppLog("Create WeatherForm Success");

	//create BookmarkPlaceForm
	count ++;
	BookmarkPlaceForm *pBookmarkPlaceForm = new BookmarkPlaceForm();
	pBookmarkPlaceForm->Initialize();
	pBookmarkPlaceForm->SetFormId(count);
	r = pFormManager->Add(BookmarkPlaceForm::GetFormId(), pBookmarkPlaceForm);
	if (IsFailed(r)) {
		return false;
	}

	AppLog("create BookmarkPlace Form Success");
	//create TrafficJamForm
	count ++;
	TrafficJamForm *pTrafficJamForm = new TrafficJamForm();
	pTrafficJamForm->Initialize();
	pTrafficJamForm->SetFormId(count);
	r = pFormManager->Add(TrafficJamForm::GetFormId(), pTrafficJamForm);
	if (IsFailed(r)) {
		return false;
	}
	AppLog("create TrafficeJam Success");
	//create BookmarkPlaceViewerForm
	count ++;
	BookmarkPlaceViewerForm *pBookmarkPlaceViewerForm = new BookmarkPlaceViewerForm();
	pBookmarkPlaceViewerForm->Initialize();
	pBookmarkPlaceViewerForm->SetFormId(count);
	r = pFormManager->Add(BookmarkPlaceViewerForm::GetFormId(), pBookmarkPlaceViewerForm);
	if (IsFailed(r)) {
		return false;
	}
	AppLog("create BookmarkPlaceViewer Success");

	//create  show find location result
	count ++;
	ShowFindLocationResultForm *pShowFileLocationResult = new ShowFindLocationResultForm();
	pShowFileLocationResult->Initialize();
	pShowFileLocationResult->SetFormId(count);
	r = pFormManager->Add(ShowFindLocationResultForm::GetFormId(), pShowFileLocationResult);
	if (IsFailed(r)) {
		return false;
	}
	AppLog("Create ShowFindLocationResult success");
	count ++;
	SearchPlaceForm *pSearchPlaceForm = new SearchPlaceForm();
	pSearchPlaceForm->Initialize();
	pSearchPlaceForm->SetFormId(count);
	r = pFormManager->Add(SearchPlaceForm::GetFormId(), pSearchPlaceForm);
	if (IsFailed(r)) {
		return false;
	}
	AppLog("Create SearchFindLocation success");

	count ++;
	SearchPlaceByPlaceForm*pSearchPlaceByPlaceForm=new SearchPlaceByPlaceForm();
	pSearchPlaceByPlaceForm->Initialize();
	pSearchPlaceByPlaceForm->SetFormId(count);
	r=pFormManager->Add(SearchPlaceByPlaceForm::GetFormId(), pSearchPlaceByPlaceForm);

	AppLog("Create SearchPlaceByPlaceForm");
	count ++;
	Form1 *pLoginTwitter=new Form1();
	pLoginTwitter->Initialize();
	pLoginTwitter->SetFormId(count);
	r=pFormManager->Add(Form1::GetFormId(), pLoginTwitter);
	AppLog("Create Login Twitter Form success");

	count ++;
	TwitterForm *pTwitterForm=new TwitterForm();
	pTwitterForm->Initialize();
	pTwitterForm->SetFormId(count);
	r = pFormManager->Add(TwitterForm::GetFormId(), pTwitterForm);
	if (IsFailed(r)) {
		return false;
	}
	AppLog("Create TwitterForm Success");

	count ++;
	MyFollowingForm *pTwitterFollowingForm=new MyFollowingForm();
	pTwitterFollowingForm->Initialize();
	pTwitterFollowingForm->SetFormId(count);
	r = pFormManager->Add(MyFollowingForm::GetFormId(), pTwitterFollowingForm);
	if (IsFailed(r)) {
		return false;
	}
	AppLog("Create TwitterFollowing Sucess");

	count ++;
	MyProfileForm*pMyProfileForm=new MyProfileForm();
	pMyProfileForm->Initialize();
	pMyProfileForm->SetFormId(count);
	r = pFormManager->Add(MyProfileForm::GetFormId(), pMyProfileForm);
	if (IsFailed(r)) {
		return false;
	}
	AppLog("Create MyProfileForm Sucess");

	count ++;
	TwitterReplyForm *pTwitterReplyForm=new TwitterReplyForm();
	pTwitterReplyForm->Initialize();
	pTwitterReplyForm->SetFormId(count);
	r = pFormManager->Add(TwitterReplyForm::GetFormId(), pTwitterReplyForm);
	if (IsFailed(r)) {
		return false;
	}
	AppLog("Create Twiter Reply Form Success");

	count ++;
	TwitterUsernameProfileForm *pTwitterUseNameProfileForm=new TwitterUsernameProfileForm();
	pTwitterUseNameProfileForm->Initialize();
	pTwitterUseNameProfileForm->SetFormId(count);
	r=pFormManager->Add(TwitterUsernameProfileForm::GetFormId(), pTwitterUseNameProfileForm);
	if(IsFailed(r))
	{
		return false;
	}
	AppLog("Create Twitter Use Name Profile Form Success");

	count ++;
	ShowFindDirectionResultForm *pShowFindDirectionResultForm = new ShowFindDirectionResultForm();
	pShowFindDirectionResultForm->Initialize();
	pShowFindDirectionResultForm->SetFormId(count);
	r = pFormManager->Add(ShowFindDirectionResultForm::GetFormId(), pShowFindDirectionResultForm);
	if(IsFailed(r))
	{
		return false;
	}
	AppLog("Create ShowFindDirectionResultForm success");

	count ++;
	Googlebuzzform *pGoogleBuzzForm = new Googlebuzzform();
	pGoogleBuzzForm->Initialize();
	pGoogleBuzzForm->SetFormId(count);
	r = pFormManager->Add(Googlebuzzform::GetFormId(), pGoogleBuzzForm);
	if (IsFailed(r)){
		return false;
	}

	AppLog("Create Googlebuzzform complete");

	count ++;
	GoogleBuzzMenuForm *pGoogleBuzzMenuForm = new GoogleBuzzMenuForm();
	pGoogleBuzzMenuForm->Initialize();
	pGoogleBuzzMenuForm->SetFormId(count);
	r = pFormManager->Add(GoogleBuzzMenuForm::GetFormId(), pGoogleBuzzMenuForm);
	if (IsFailed(r)){
		return false;
	}
	AppLog("Create GoogleBuzzMenuForm complete");

	count ++;
	FindMultipointDirectionForm *pFindMultipointDirection = new FindMultipointDirectionForm();
	pFindMultipointDirection->Initialize();
	pFindMultipointDirection->SetFormId(count);
	r = pFormManager->Add(FindMultipointDirectionForm::GetFormId(), pFindMultipointDirection);
	if (IsFailed(r)){
		return false;
	}
	AppLog("Create FindMultipointDirection complete");

	AppLog("Initialize complete");
	pFormManager->SetCurrentForm(WebForm::GetFormId());
	pWebForm->RequestRedraw(true);

	AppLog("Set currentForm complete");


	//form


	//

	//		SearchPlaceForm*pSeachPlaceForm;
	//		Form1 *pLoginForm;

	return true;

}

bool JavaMapVer3::OnAppTerminating(AppRegistry& appRegistry,
		bool forcedTermination) {
	// TODO:
	// Deallocate resources allocated by this application for termination.
	// The application's permanent data and context can be saved via appRegistry.
	AppLog("delete 1");
	delete CurrentLocationService::getInstance();
	FormManager *pFormManager = null;
	result r = FormManager::GetInstance(pFormManager);
	if (IsFailed(r)){
		return true;
	}
	AppLog("delete 2");
	delete pFormManager;
	AppLog("delete 3");
	return true;
}

void JavaMapVer3::OnForeground(void) {
	// TODO:
	// Start or resume drawing when the application is moved to the foreground.
}

void JavaMapVer3::OnBackground(void) {
	// TODO:
	// Stop drawing when the application is moved to the background.
}

void JavaMapVer3::OnLowMemory(void) {
	// TODO:
	// Free unused resources or close the application.
}

void JavaMapVer3::OnBatteryLevelChanged(BatteryLevel batteryLevel) {
	// TODO:
	// Handle any changes in battery level here.
	// Stop using multimedia features(camera, mp3 etc.) if the battery level is CRITICAL.
}

void JavaMapVer3::OnScreenOn(void) {
	// TODO:
	// Get the released resources or resume the operations that were paused or stopped in OnScreenOff().
}

void JavaMapVer3::OnScreenOff(void) {
	// TODO:
	//  Unless there is a strong reason to do otherwise, release resources (such as 3D, media, and sensors) to allow the device to enter the sleep mode to save the battery.
	// Invoking a lengthy asynchronous method within this listener method can be risky, because it is not guaranteed to invoke a callback before the device enters the sleep mode.
	// Similarly, do not perform lengthy operations in this listener method. Any operation must be a quick one.
}
